package com.wl.api.controller.business;

import java.util.Date;
import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.MessageCodeEnum;
import com.wl.api.common.utils.PageInfo;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerOrderDTO;
import com.wl.api.dto.business.CustomerUserDTO;
import com.wl.api.service.business.ICustomerOrderService;

/**
 * 客户订单接口
 * @author cj
 *
 */
@RestController
@RequestMapping("/external/customer/order")
public class CustomerOrderController extends CommonController {
	
	@Autowired
	ICustomerOrderService customerOrderService;
	
	
	
	/**
	 * 创建订单
	 * @param order
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/manage", method = RequestMethod.POST)
	public JsonObject createOrder(@RequestBody CustomerOrderDTO order) throws Exception {
		JsonObject json = JsonObject.getInstance();
		
		String title = "", code =""; 
		boolean pass = true;
		if (validate().test(order)) {
			pass = false;
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
		}
		if (pass) {
			initData(order);
			MessageCodeEnum mCode= customerOrderService.createOrder(order);
			title = mCode.getMessage();
			code = mCode.getCode();
			json.setData(order.getOrderNo());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	/**
	 * 更新订单
	 * @param order
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/manage", method = RequestMethod.PUT)
	public JsonObject updateOrder(@RequestBody CustomerOrderDTO order) throws Exception {
		JsonObject json = JsonObject.getInstance();
		
		String title = "", code =""; 
		boolean pass = true;
		if (validate().or(s -> StringUtil.isEmpty(s.getOrderNo())).test(order)) {
			pass = false;
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
		}
		if (pass) {
			updateData(order);
			MessageCodeEnum mCode= customerOrderService.updateOrder(order);
			title = mCode.getMessage();
			code = mCode.getCode();
			json.setData(order.getOrderNo());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public JsonObject queryOrderItem(CustomerOrderDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		param.setCustId(user.getCustId());
		param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(), param.getPageSize());
		PageInfo<CustomerOrderDTO> pageInfo =  customerOrderService.queryOrderPage(param);
		json.setPage(pageInfo);
		setJsonMessage(json);
		return json;
	}
	@RequestMapping(value = "/manage/{orderNo}", method = RequestMethod.GET)
	public JsonObject getOrderDetail(@PathVariable("orderNo") String orderNo) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		customerOrderService.getOrderDetail(CustomerOrderDTO.builder().custId(user.getCustId()).orderNo(orderNo).build());
		setJsonMessage(json);
		return json;
	}
	
	private void initData(CustomerOrderDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.setPaytime(new Date());
		param.setCustId(user.getCustId());
	}
	private void updateData(CustomerOrderDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildUpdateMsg(user.getId(), user.getUsername());
	}
	
	private Predicate<CustomerOrderDTO> validate() {
		return x ->
				(x.getNetworkId() == null && x.getServicePickupId() == null)
		        || Objects.isNull(x.getChannelId()) || Objects.isNull(x.getFromId())
				|| Objects.isNull(x.getReachId()) || Objects.isNull(x.getWeight()) || Objects.isNull(x.getOrderDetials()) 
				|| x.getOrderDetials().parallelStream().anyMatch(a -> Objects.isNull(a.getProductId()) || Objects.isNull(a.getProductQty()));
	}
}
